package com.ylkj.user.controller;

import com.ylkj.common.security.md5.MD5;
import com.ylkj.common.util.Base64Utils;
import com.ylkj.common.util.bean.BeanMapUtil;
import com.ylkj.core.BaesController;
import com.ylkj.core.page.IGenericPage;
import com.ylkj.core.page.Page;
import com.ylkj.core.vo.JsonResult;
import com.ylkj.core.vo.QueryResult;
import com.ylkj.core.vo.TipMessage;
import com.ylkj.user.model.TalentAdmin;
import com.ylkj.user.services.AdminService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 用户管理
 */
@Controller
@RequestMapping(value = "/admin/")
public class AdminController extends BaesController {
    private Logger logger = LoggerFactory.getLogger(AdminController.class);

    @Autowired
    private AdminService adminService;



    /**
     * 用户列表页(分页)
     *
     * @return
     */
    @RequestMapping(value = "page.do")
    //@RequestMapping(value = "page", method = RequestMethod.POST)
    @ResponseBody
    public Object page(Map map, HttpServletRequest request) {
        logger.info("获取用户列表信息(分页):开始");
        try {
            map = getQueryParams(request);
            TalentAdmin param = (TalentAdmin) BeanMapUtil.Map2Bean(TalentAdmin.class, map);

            Page page = (Page) BeanMapUtil.Map2Bean(Page.class, map);
            IGenericPage<TalentAdmin> list = adminService.findPageBy(param, page.getOffset(), page.getLimit(), "id", "desc");
            return this.buildQueryResult(true,list.getThisPageElements(),list.getTotalCount(),page.getPageSize(),page.getPage(), list.getTotalPage());
        } catch (Exception e) {
            // TODO: handle
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.buildQueryResult(null,0);
        }finally {
            logger.info("获取用户列表信息(分页):结束");
        }
    }

    /**
     * 用户列表页
     *
     * @return
     */
    @RequestMapping(value = "adminlist.do")
    //@RequestMapping(value = "list", method = RequestMethod.POST)
    @ResponseBody
    public Object list(Map map, HttpServletRequest request) {
        logger.info("获取用户列表信息:开始");
        try {
            map = getQueryParams(request);
            TalentAdmin param = (TalentAdmin) BeanMapUtil.Map2Bean(TalentAdmin.class, map);
            List<TalentAdmin> list = adminService.findListBy(param, "id", "desc");
            return list;
        } catch (Exception e) {
            // TODO: handle
            e.printStackTrace();
            logger.error(e.getMessage());
            return null;
        }finally {
            logger.info("获取用户列表信息:结束");
        }
    }

    @RequestMapping(value = "addUser.do", method = RequestMethod.POST)
    @ResponseBody
    public Object save(@RequestBody TalentAdmin param, HttpServletRequest request) {
        logger.info("获取用户列表信息:开始");
        try {
            TalentAdmin talentAdmin = adminService.getByUid(param.getAdmin_uid());
            if(talentAdmin!=null&&talentAdmin.getStatus_frist()==99){
                String uid = MD5.GetMD5Code(UUID.randomUUID().toString());
                param.setUid(uid);
                param.setPassword(Base64Utils.encode(param.getPassword().getBytes()));
                return adminService.save(param);
            }
            return 0;
        } catch (Exception e) {
            // TODO: handle
            e.printStackTrace();
            logger.error(e.getMessage());
            return null;
        }finally {
            logger.info("获取用户列表信息:结束");
        }
    }

    @RequestMapping(value = "altUser.do", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@RequestBody TalentAdmin param, HttpServletRequest request) {
        logger.info("修改用户密码:开始");
        try {
            TalentAdmin talentAdmin = adminService.getByUid(param.getAdmin_uid());
            //判断只有本人和管理员可以修改用户信息，密码。
            if(talentAdmin!=null&&(talentAdmin.getStatus_frist()==99||talentAdmin.getAdmin_uid().equals(talentAdmin.getUid()))){
                param.setPassword(Base64Utils.encode(param.getPassword().getBytes()));
                return adminService.update(param);
            }
            return 0;
        } catch (Exception e) {
            // TODO: handle
            e.printStackTrace();
            logger.error(e.getMessage());
            return null;
        }finally {
            logger.info("修改用户密码:结束");
        }
    }


    @RequestMapping(value = "delUser.do", method = RequestMethod.POST)
    @ResponseBody
    public Object del(@RequestBody TalentAdmin param, HttpServletRequest request) {
        logger.info("获取用户列表信息:开始");
        try {
            String uid = param.getUid();
            String admin_uid = param.getAdmin_uid();
            TalentAdmin talentAdmin = adminService.getByUid(admin_uid);
            if(talentAdmin!=null&&talentAdmin.getStatus_frist()==99){
                return adminService.deleteByUId(uid);
            }
            return 0;
        } catch (Exception e) {
            // TODO: handle
            e.printStackTrace();
            logger.error(e.getMessage());
            return null;
        }finally {
            logger.info("获取用户列表信息:结束");
        }
    }

    @RequestMapping(value = "login.do", method = RequestMethod.POST)
    @ResponseBody
    public Object login(@RequestBody TalentAdmin param, HttpServletRequest request) {
        logger.info("获取用户登录信息:开始");
        try {
            String mobile = param.getMobile();
            String password = param.getPassword();
            TalentAdmin talentAdmin = adminService.getByMobile(mobile);
            if(talentAdmin != null && talentAdmin.getPassword().equals(Base64Utils.encode(password.getBytes()))){
                   return talentAdmin;
            }
            return null;
        } catch (Exception e) {
            // TODO: handle
            e.printStackTrace();
            logger.error(e.getMessage());
            return null;
        }finally {
            logger.info("获取用户登录信息:结束");
        }
    }

}
